package com.paomi.client.activity.login.presenter;

import android.text.TextUtils;

import com.hannesdorfmann.mosby3.mvp.MvpBasePresenter;
import com.hannesdorfmann.mosby3.mvp.MvpView;
import com.paomi.client.activity.login.view.LoginMvpView;
import com.paomi.client.bean.BaseJSON;
import com.paomi.client.bean.UserInfo;
import com.paomi.client.bean.UserInfoEntity;
import com.paomi.client.net.Api;
import com.paomi.client.net.BaseCallback;
import com.paomi.client.net.Constants;
import com.paomi.client.utils.RxDataUtils;
import com.paomi.client.utils.RxRegUtils;
import com.paomi.client.view.LoadingDialog;
import com.paomi.client.widget.RxToast;

import org.greenrobot.eventbus.EventBus;

import retrofit2.Call;


/**
 * Created by pc on 2018/1/12.
 */

public class LoginPresenter extends MvpBasePresenter<LoginMvpView> {
    /**
     * 请求发送登录短信验证码
     * @param phone
     */
    public void setSendMsg(final String phone){
        if (!RxDataUtils.isEmpty(phone)) {
            if (phone.length() < 6 || phone.length() > 12||!RxRegUtils.isMobileExact(phone)) {
                RxToast.info("请输入正确的手机号");
            } else {
                //第一种网络请求方式
                Api.getInstanceService(Constants.Login_URL).sendMessage(phone).enqueue(new BaseCallback<BaseJSON>() {
                    @Override
                    public void onResponse(Call<BaseJSON> call, BaseJSON result) {
//                        EventBus.getDefault().post(result);
                        getView().showSendMsg(result);
                    }
                });
            }
        } else {
            RxToast.info("请输入手机号");
        }

    }
    /**
     * 请求登录接口
     * @param phone
     * @param password
     */
    public void setLogin(final String phone, final String password){
        if(TextUtils.isEmpty(password)){
            RxToast.info("请输入密码");
            return;
        }
        if (!RxDataUtils.isEmpty(phone)) {
            if (phone.length() < 6 || phone.length() > 12||!RxRegUtils.isMobileExact(phone)) {
                RxToast.info("请输入正确的手机号");
            } else {
                LoadingDialog.get().showLoading();
                //第一种网络请求方式
                Api.getInstanceService(Constants.Login_URL).login(phone,password,"2").enqueue(new BaseCallback<UserInfo>() {
                    @Override
                    public void onResponse(Call<UserInfo> call, UserInfo result) {
//                        EventBus.getDefault().post(result);
                        getView().showLoginData(result);

                    }
                });
            }
        } else {
            RxToast.info("请输入手机号");
        }
    }

    /**
     *获取用户信息
     */
    public void getUserInfo(){
        Api.getInstanceService(Constants.User_URL).getUserInfo().enqueue(new BaseCallback<UserInfoEntity>() {
            @Override
            public void onResponse(Call<UserInfoEntity> call, UserInfoEntity result) {
                getView().showUserInfo(result.getInfo());
                LoadingDialog.get().hideLoading();
            }
        });
    }
}
